/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cdinfusion.data.CDElement;
import org.cdinfusion.data.CDFastaHeader;
import org.cdinfusion.data.CDValidator;
import org.cdinfusion.data.environment.Air;
import org.cdinfusion.data.environment.Biofilm;
import org.cdinfusion.data.environment.Environment;
import org.cdinfusion.data.environment.Host_associated;
import org.cdinfusion.data.environment.Human_associated;
import org.cdinfusion.data.environment.Human_gut;
import org.cdinfusion.data.environment.Human_oral;
import org.cdinfusion.data.environment.Human_skin;
import org.cdinfusion.data.environment.Human_vaginal;
import org.cdinfusion.data.environment.Miscellaneous;
import org.cdinfusion.data.environment.Plant_associated;
import org.cdinfusion.data.environment.Sediment;
import org.cdinfusion.data.environment.Soil;
import org.cdinfusion.data.environment.Wastewater;
import org.cdinfusion.data.environment.Water;
import org.cdinfusion.data.studytype.BA;
import org.cdinfusion.data.studytype.EU;
import org.cdinfusion.data.studytype.ME;
import org.cdinfusion.data.studytype.MIMARKS_C;
import org.cdinfusion.data.studytype.MIMARKS_S;
import org.cdinfusion.data.studytype.ORG;
import org.cdinfusion.data.studytype.PL;
import org.cdinfusion.data.studytype.StudyType;
import org.cdinfusion.data.studytype.VI;
import org.cdinfusion.filehandling.CSVHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class CD_to_Map_or_3b1_Controller extends AbstractController {
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("##### CD_to_Map_or_3b1_Controller #####");
		// Variables and objects from existing context
		HttpSession session = request.getSession();
		String submitbutton = request.getParameter("submit");
		String origin = (String) session.getAttribute("origin");
		String context_exists = (String) session.getAttribute("context_exists");
		String opt_2b = (String) session.getAttribute("opt_2b");
		boolean keepheader = Boolean.parseBoolean(request
				.getParameter("KeepFASTAHeader"));
		List<CDElement> cdeLIST = (List<CDElement>) session
				.getAttribute("cdeLIST");
		List<CDElement> old_cdeLIST = new ArrayList<CDElement>();

		List<String> allSequenceIDs = (List<String>) session
				.getAttribute("allSequenceIDs");
		// sSeqID contains all via checkboxes or fileupload selected seqIDs
		ArrayList<String> sSeqID = (ArrayList<String>) session
				.getAttribute("sSeqID");

		// uSeqID contains all already used seqIDs
		List<String> uSeqID = (List<String>) session.getAttribute("uSeqID");

		// SAVING the cd set
		String study_type = (String) request.getParameter("study_type");
		String env_type = (String) request.getParameter("env_type");

		CDElement cdelement = null;
		StudyType studytype = null;
		Environment environment = null;
		CDFastaHeader fastaheader = null;
		CDValidator valide = new CDValidator();
		String lat_lon = "";
		String latlon = "";
		String date = "";
		String pcr_primers = "";
		String organism = request.getParameter("organism");

		if (organism.equals("")) {
			organism = "uncultured organism";
		}
		try {
			fastaheader = new CDFastaHeader(
					request.getParameter("collectedby"),
					request.getParameter("authority"), organism,
					request.getParameter("isolation_source"));
			valide.useOfNonAsciiFH(fastaheader);
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}

		try {
			if (study_type.equals("mimarks_s")) {
				int lib_size = 0;
				if (!request.getParameter("lib_size").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("lib_size"));
					lib_size = Integer.parseInt(request
							.getParameter("lib_size"));
				}
				int lib_reads_seqd = 0;
				if (!request.getParameter("lib_reads_seqd").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("lib_reads_seqd"));
					lib_reads_seqd = Integer.parseInt(request
							.getParameter("lib_reads_seqd"));
				}
				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				pcr_primers = request.getParameter("pcr_primers");
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);
				pcr_primers = valide.correctValueOfPrimer(pcr_primers);

				MIMARKS_S mimarks_s = new MIMARKS_S(
						request.getParameter("adapters"),
						request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("lib_const_meth"),
						request.getParameter("material"),
						request.getParameter("project_name"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), lat_lon,
						date, request.getParameter("alt_elev"), lib_size,
						request.getParameter("seq_meth"),
						request.getParameter("assembly"), lib_reads_seqd,
						request.getParameter("lib_vector"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("lib_screen"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("samp_mat_process"),
						request.getParameter("seq_quality_check"),
						request.getParameter("sop"),
						request.getParameter("url"),
						request.getParameter("experimental_factor"),
						request.getParameter("rel_to_oxygen"),
						request.getParameter("samp_collect_device"),
						request.getParameter("target_gene"),
						request.getParameter("target_subfragment"),
						pcr_primers, request.getParameter("chimera_check"),
						request.getParameter("pcr_cond"),
						request.getParameter("mid"));
				studytype = mimarks_s;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			
			return maverr;
		}

		try {
			if (study_type.equals("mimarks_c")) {

				int extrachrom_elements = 0;
				if (!request.getParameter("extrachrom_elements").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("extrachrom_elements"));
					extrachrom_elements = Integer.parseInt(request
							.getParameter("extrachrom_elements"));
				}
				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				pcr_primers = request.getParameter("pcr_primers");
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);
				pcr_primers = valide.correctValueOfPrimer(pcr_primers);

				MIMARKS_C mimarks_c = new MIMARKS_C(
						request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), date,
						request.getParameter("project_name"), lat_lon,
						request.getParameter("trophic_level"),
						request.getParameter("alt_elev"),
						request.getParameter("seq_meth"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("source_mat_id"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("subspecf_gen_lin"),
						request.getParameter("isol_growth_condt"),
						request.getParameter("samp_mat_process"),
						request.getParameter("biotic_relationship"),
						request.getParameter("seq_quality_check"),
						request.getParameter("sop"),
						request.getParameter("url"),
						request.getParameter("samp_collect_device"),
						extrachrom_elements,
						request.getParameter("target_gene"), pcr_primers,
						request.getParameter("target_subfragment"),
						request.getParameter("chimera_check"),
						request.getParameter("pcr_cond"),
						request.getParameter("rel_to_oxygen"),
						request.getParameter("experimental_factor"));
				studytype = mimarks_c;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (study_type.equals("eu")) {

				int num_replicons = 0;
				if (!request.getParameter("num_replicons").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("num_replicons"));
					num_replicons = Integer.parseInt(request
							.getParameter("num_replicons"));
				}
				int extrachrom_elements = 0;
				if (!request.getParameter("extrachrom_elements").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("extrachrom_elements"));
					extrachrom_elements = Integer.parseInt(request
							.getParameter("extrachrom_elements"));
				}
				int host_spec_range = 0;
				if (!request.getParameter("host_spec_range").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("host_spec_range"));
					host_spec_range = Integer.parseInt(request
							.getParameter("host_spec_range"));
				}
				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);

				EU eu = new EU(request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), date,
						num_replicons, request.getParameter("ploidy"),
						request.getParameter("project_name"),
						request.getParameter("propagation"), lat_lon,
						request.getParameter("trophic_level"),
						request.getParameter("alt_elev"),
						request.getParameter("pathogenicity"),
						request.getParameter("assembly"),
						request.getParameter("assembly_name"),
						request.getParameter("seq_meth"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("source_mat_id"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("subspecf_gen_lin"),
						host_spec_range, request.getParameter("specific_host"),
						request.getParameter("health_disease_stat"),
						request.getParameter("ref_biomaterial"),
						request.getParameter("estimated_size"),
						request.getParameter("isol_growth_condt"),
						request.getParameter("samp_mat_process"),
						request.getParameter("biotic_relationship"),
						request.getParameter("finishing_strategy"),
						request.getParameter("sop"),
						request.getParameter("annot_source"),
						request.getParameter("url"),
						request.getParameter("samp_collect_device"),
						extrachrom_elements,
						request.getParameter("experimental_factor"),
						request.getParameter("adapters"));
				studytype = eu;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (study_type.equals("ba")) {

				int num_replicons = 0;
				if (!request.getParameter("num_replicons").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("num_replicons"));
					num_replicons = Integer.parseInt(request
							.getParameter("num_replicons"));
				}
				int extrachrom_elements = 0;
				if (!request.getParameter("extrachrom_elements").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("extrachrom_elements"));
					extrachrom_elements = Integer.parseInt(request
							.getParameter("extrachrom_elements"));
				}
				int host_spec_range = 0;
				if (!request.getParameter("host_spec_range").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("host_spec_range"));
					host_spec_range = Integer.parseInt(request
							.getParameter("host_spec_range"));
				}
				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);

				BA ba = new BA(request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"),
						request.getParameter("project_name"), date,
						request.getParameter("experimental_factor"), lat_lon,
						request.getParameter("alt_elev"),
						request.getParameter("subspecf_gen_lin"),
						num_replicons, extrachrom_elements,
						request.getParameter("estimated_size"),
						request.getParameter("ref_biomaterial"),
						request.getParameter("source_mat_id"),
						request.getParameter("pathogenicity"),
						request.getParameter("biotic_relationship"),
						request.getParameter("specific_host"), host_spec_range,
						request.getParameter("health_disease_stat"),
						request.getParameter("trophic_level"),
						request.getParameter("encoded_traits"),
						request.getParameter("rel_to_oxygen"),
						request.getParameter("isol_growth_condt"),
						request.getParameter("samp_collect_device"),
						request.getParameter("samp_mat_process"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("adapters"),
						request.getParameter("seq_meth"),
						request.getParameter("assembly"),
						request.getParameter("assembly_name"),
						request.getParameter("finishing_strategy"),
						request.getParameter("annot_source"),
						request.getParameter("url"),
						request.getParameter("sop"));
				studytype = ba;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (study_type.equals("pl")) {
				int host_spec_range = 0;
				if (!request.getParameter("host_spec_range").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("host_spec_range"));
					host_spec_range = Integer.parseInt(request
							.getParameter("host_spec_range"));
				}
				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);

				PL pl = new PL(request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), date,
						request.getParameter("project_name"),
						request.getParameter("experimental_factor"), lat_lon,
						request.getParameter("alt_elev"),
						request.getParameter("subspecf_gen_lin"),
						request.getParameter("estimated_size"),
						request.getParameter("ref_biomaterial"),
						request.getParameter("source_mat_id"),
						request.getParameter("specific_host"), host_spec_range,
						request.getParameter("propagation"),
						request.getParameter("encoded_traits"),
						request.getParameter("isol_growth_condt"),
						request.getParameter("samp_collect_device"),
						request.getParameter("samp_mat_process"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("adapters"),
						request.getParameter("seq_meth"),
						request.getParameter("assembly"),
						request.getParameter("assembly_name"),
						request.getParameter("finishing_strategy"),
						request.getParameter("annot_source"),
						request.getParameter("url"),
						request.getParameter("sop"));
				studytype = pl;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			
			return maverr;
		}
		try {
			if (study_type.equals("vi")) {

				int num_replicons = 0;
				if (!request.getParameter("num_replicons").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("num_replicons"));
					num_replicons = Integer.parseInt(request
							.getParameter("num_replicons"));
				}
				int host_spec_range = 0;
				if (!request.getParameter("host_spec_range").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("host_spec_range"));
					host_spec_range = Integer.parseInt(request
							.getParameter("host_spec_range"));
				}
				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				valide.correctValueOfLatLon(latlon);
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);

				VI vi = new VI(request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), date,
						num_replicons, request.getParameter("project_name"),
						request.getParameter("propagation"), lat_lon,
						request.getParameter("alt_elev"),
						request.getParameter("pathogenicity"),
						request.getParameter("assembly"),
						request.getParameter("seq_meth"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("source_mat_id"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("subspecf_gen_lin"),
						host_spec_range, request.getParameter("specific_host"),
						request.getParameter("health_disease_stat"),
						request.getParameter("encoded_traits"),
						request.getParameter("ref_biomaterial"),
						request.getParameter("estimated_size"),
						request.getParameter("isol_growth_condt"),
						request.getParameter("samp_mat_process"),
						request.getParameter("biotic_relationship"),
						request.getParameter("finishing_strategy"),
						request.getParameter("sop"),
						request.getParameter("url"),
						request.getParameter("samp_collect_device"),
						request.getParameter("experimental_factor"),
						request.getParameter("adapters"),
						request.getParameter("assembly_name"),
						request.getParameter("annot_source"));
				studytype = vi;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (study_type.equals("orga") | study_type.equals("org")) {
				int extrachrom_elements = 0;
				if (!request.getParameter("extrachrom_elements").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("extrachrom_elements"));
					extrachrom_elements = Integer.parseInt(request
							.getParameter("extrachrom_elements"));
				}

				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				valide.correctValueOfLatLon(latlon);
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);

				ORG orga = new ORG(request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), date,
						request.getParameter("project_name"), lat_lon,
						request.getParameter("alt_elev"),
						request.getParameter("assembly_name"),
						request.getParameter("assembly"),
						request.getParameter("seq_meth"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("source_mat_id"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("subspecf_gen_lin"),
						request.getParameter("ref_biomaterial"),
						request.getParameter("estimated_size"),
						request.getParameter("isol_growth_condt"),
						request.getParameter("samp_mat_process"),
						request.getParameter("finishing_strategy"),
						request.getParameter("sop"),
						request.getParameter("url"),
						request.getParameter("samp_collect_device"),
						request.getParameter("adapters"),
						request.getParameter("annot_source"),
						request.getParameter("experimental_factor"),
						extrachrom_elements);
				studytype = orga;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (study_type.equals("me")) {

				int lib_size = 0;
				if (!request.getParameter("lib_size").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("lib_size"));
					lib_size = Integer.parseInt(request
							.getParameter("lib_size"));
				}
				int lib_reads_seqd = 0;
				if (!request.getParameter("lib_size").equals("")) {
					valide.correctValueOfIntegerFields(request
							.getParameter("lib_reads_seqd"));
					lib_reads_seqd = Integer.parseInt(request
							.getParameter("lib_reads_seqd"));
				}

				latlon = request.getParameter("lat_lon");
				date = request.getParameter("collection_date");
				lat_lon = valide.correctValueOfLatLon(latlon);
				valide.correctValueOfDateAndTime(date);

				ME me = new ME(request.getParameter("biome"),
						request.getParameter("depth"),
						request.getParameter("feature"),
						request.getParameter("geo_loc_name"),
						request.getParameter("lib_const_meth"),
						request.getParameter("material"),
						request.getParameter("samp_size"),
						request.getParameter("submitted_to_insdc"), date,
						request.getParameter("project_name"), lat_lon,
						request.getParameter("alt_elev"), lib_size,
						request.getParameter("assembly"),
						request.getParameter("seq_meth"), lib_reads_seqd,
						request.getParameter("lib_vector"),
						request.getParameter("nucl_acid_amp"),
						request.getParameter("source_mat_id"),
						request.getParameter("nucl_acid_ext"),
						request.getParameter("ref_biomaterial"),
						request.getParameter("samp_mat_process"),
						request.getParameter("finishing_strategy"),
						request.getParameter("sop"),
						request.getParameter("url"),
						request.getParameter("lib_screen"),
						request.getParameter("samp_collect_device"),
						request.getParameter("mid"),
						request.getParameter("rel_to_oxygen"),
						request.getParameter("experimental_factor"),
						request.getParameter("adapters"),
						request.getParameter("assembly_name"),
						request.getParameter("annot_source"));
				studytype = me;
				valide.useOfNonAsciiStudy(studytype);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		// ENVTYPE

		try {
			if (env_type.equals("water")) {
				Water water = new Water(request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("diss_org_carb"),
						request.getParameter("diss_org_nitro"),
						request.getParameter("elev"),
						request.getParameter("sodium"),
						request.getParameter("temp"),
						request.getParameter("tidal_stage"),
						request.getParameter("tot_diss_nitro"),
						request.getParameter("diether_lipids"),
						request.getParameter("aminopept_act"),
						request.getParameter("atmospheric_data"),
						request.getParameter("bacteria_carb_prod"),
						request.getParameter("alkalinity"),
						request.getParameter("ammonium"),
						request.getParameter("calcium"),
						request.getParameter("carb_nitro_ratio"),
						request.getParameter("chlorophyll"),
						request.getParameter("density"),
						request.getParameter("diss_oxygen"),
						request.getParameter("chem_administration"),
						request.getParameter("chloride"),
						request.getParameter("current"),
						request.getParameter("diss_carb_dioxide"),
						request.getParameter("diss_hydrogen"),
						request.getParameter("diss_inorg_phosp"),
						request.getParameter("diss_inorg_nitro"),
						request.getParameter("diss_inorg_carb"),
						request.getParameter("alkyl_diethers"),
						request.getParameter("bishomohopanol"),
						request.getParameter("biomass"),
						request.getParameter("bromide"),
						request.getParameter("glucosidase_act"),
						request.getParameter("light_intensity"),
						request.getParameter("magnesium"),
						request.getParameter("mean_frict_vel"),
						request.getParameter("mean_peak_frict_vel"),
						request.getParameter("nitrite"),
						request.getParameter("nitro"),
						request.getParameter("org_nitro"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("part_org_carb"),
						request.getParameter("part_org_nitro"),
						request.getParameter("n_alkanes"),
						request.getParameter("nitrate"),
						request.getParameter("org_carb"),
						request.getParameter("org_matter"),
						request.getParameter("perturbation"),
						request.getParameter("photon_flux"),
						request.getParameter("ph"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("potassium"),
						request.getParameter("petroleum_hydrocarb"),
						request.getParameter("phosphate"),
						request.getParameter("phosplipid_fatt_acid"),
						request.getParameter("pressure"),
						request.getParameter("primary_prod"),
						request.getParameter("phaeopigments"),
						request.getParameter("redox_potential"),
						request.getParameter("salinity"),
						request.getParameter("silicate"),
						request.getParameter("soluble_react_phosp"),
						request.getParameter("suspend_part_matter"),
						request.getParameter("sulfate"),
						request.getParameter("sulfide"),
						request.getParameter("tot_depth_water_col"),
						request.getParameter("tot_inorg_nitro"),
						request.getParameter("tot_part_carb"),
						request.getParameter("tot_phosp"),
						request.getParameter("organism_count"),
						request.getParameter("misc_param"),
						request.getParameter("tot_nitro"));
				environment = water;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("wastewater")) {
				Wastewater wastewater = new Wastewater(
						request.getParameter("depth"),
						request.getParameter("sodium"),
						request.getParameter("temp"),
						request.getParameter("alkalinity"),
						request.getParameter("chem_administration"),
						request.getParameter("chem_oxygen_dem"),
						request.getParameter("efficiency_percent"),
						request.getParameter("emulsions"),
						request.getParameter("gaseous_substances"),
						request.getParameter("biochem_oxygen_dem"),
						request.getParameter("indust_eff_percent"),
						request.getParameter("inorg_particles"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("nitrate"),
						request.getParameter("org_particles"),
						request.getParameter("perturbation"),
						request.getParameter("ph"),
						request.getParameter("samp_salinity"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("phosphate"),
						request.getParameter("pre_treatment"),
						request.getParameter("reactor_type"),
						request.getParameter("secondary_treatment"),
						request.getParameter("sewage_type"),
						request.getParameter("sludge_retent_time"),
						request.getParameter("primary_treatment"),
						request.getParameter("soluble_inorg_mat"),
						request.getParameter("soluble_org_mat"),
						request.getParameter("suspend_solids"),
						request.getParameter("tertiary_treatment"),
						request.getParameter("tot_phosphate"),
						request.getParameter("wastewater_type"),
						request.getParameter("tot_nitro"),
						request.getParameter("organism_count"),
						request.getParameter("misc_param"));
				environment = wastewater;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("soil")) {
				Soil soil = new Soil(request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("elev"),
						request.getParameter("cur_vegetation"),
						request.getParameter("sieving"),
						request.getParameter("drainage_class"),
						request.getParameter("extreme_salinity"),
						request.getParameter("al_sat"),
						request.getParameter("heavy_metals"),
						request.getParameter("agrochem_addition"),
						request.getParameter("tillage"),
						request.getParameter("fire"),
						request.getParameter("flooding"),
						request.getParameter("extreme_event"),
						request.getParameter("other"),
						request.getParameter("horizon"),
						request.getParameter("annual_season_temp"),
						request.getParameter("annual_season_precpt"),
						request.getParameter("link_class_info"),
						request.getParameter("microbial_biomass"),
						request.getParameter("link_addit_analys"),
						request.getParameter("crop_rotation"),
						request.getParameter("local_class"),
						request.getParameter("soil_type"),
						request.getParameter("slope_gradient"),
						request.getParameter("slope_aspect"),
						request.getParameter("profile_position"),
						request.getParameter("ph"),
						request.getParameter("fao_class"),
						request.getParameter("store_cond"),
						request.getParameter("texture"),
						request.getParameter("tot_org_carb"),
						request.getParameter("tot_n"),
						request.getParameter("water_content_soil"),
						request.getParameter("cur_land_use"),
						request.getParameter("previous_land_use"),
						request.getParameter("horizon_meth"),
						request.getParameter("water_content_soil_meth"),
						request.getParameter("samp_weight_dna_ext"),
						request.getParameter("soil_type_meth"),
						request.getParameter("texture_meth"),
						request.getParameter("ph_meth"),
						request.getParameter("misc_param"),
						request.getParameter("cur_vegetation_meth"),
						request.getParameter("previous_land_use_meth"),
						request.getParameter("pool_dna_extracts"),
						request.getParameter("link_climate_info"),
						request.getParameter("local_class_meth"),
						request.getParameter("tot_org_c_meth"),
						request.getParameter("tot_n_meth"),
						request.getParameter("microbial_biomass_meth"),
						request.getParameter("salinity_meth"),
						request.getParameter("heavy_metals_meth"),
						request.getParameter("al_sat_meth"));
				environment = soil;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("sediment")) {
				Sediment sediment = new Sediment(request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("diss_inorg_carb"),
						request.getParameter("diss_org_nitro"),
						request.getParameter("elev"),
						request.getParameter("sodium"),
						request.getParameter("temp"),
						request.getParameter("tidal_stage"),
						request.getParameter("alkalinity"),
						request.getParameter("aminopept_act"),
						request.getParameter("ammonium"),
						request.getParameter("bacteria_carb_prod"),
						request.getParameter("calcium"),
						request.getParameter("carb_nitro_ratio"),
						request.getParameter("chlorophyll"),
						request.getParameter("density"),
						request.getParameter("diether_lipids"),
						request.getParameter("diss_carb_dioxide"),
						request.getParameter("diss_hydrogen"),
						request.getParameter("diss_org_carb"),
						request.getParameter("diss_oxygen"),
						request.getParameter("chem_administration"),
						request.getParameter("chloride"),
						request.getParameter("alkyl_diethers"),
						request.getParameter("bishomohopanol"),
						request.getParameter("biomass"),
						request.getParameter("bromide"),
						request.getParameter("glucosidase_act"),
						request.getParameter("org_nitro"),
						request.getParameter("magnesium"),
						request.getParameter("mean_frict_vel"),
						request.getParameter("mean_peak_frict_vel"),
						request.getParameter("nitrite"),
						request.getParameter("nitro"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("particle_class"),
						request.getParameter("part_org_carb"),
						request.getParameter("n_alkanes"),
						request.getParameter("nitrate"),
						request.getParameter("org_carb"),
						request.getParameter("org_matter"),
						request.getParameter("perturbation"),
						request.getParameter("phosphate"),
						request.getParameter("ph"),
						request.getParameter("porosity"),
						request.getParameter("pressure"),
						request.getParameter("redox_potential"),
						request.getParameter("salinity"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("sediment_type"),
						request.getParameter("silicate"),
						request.getParameter("potassium"),
						request.getParameter("petroleum_hydrocarb"),
						request.getParameter("phosplipid_fatt_acid"),
						request.getParameter("phaeopigments"),
						request.getParameter("sulfate"),
						request.getParameter("sulfide"),
						request.getParameter("tot_carb"),
						request.getParameter("tot_org_carb"),
						request.getParameter("turbidity"),
						request.getParameter("water_content"),
						request.getParameter("organism_count"),
						request.getParameter("methane"),
						request.getParameter("misc_param"),
						request.getParameter("tot_nitro"));
				environment = sediment;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("plant_associated")) {
				Plant_associated plant_associated = new Plant_associated(
						request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("elev"),
						request.getParameter("temp"),
						request.getParameter("dry_mass"),
						request.getParameter("chem_administration"),
						request.getParameter("chem_mutagen"),
						request.getParameter("climate_environment"),
						request.getParameter("disease_stat"),
						request.getParameter("fertilizer_regm"),
						request.getParameter("fungicide_regm"),
						request.getParameter("gaseous_environment"),
						request.getParameter("antibiotic_regm"),
						request.getParameter("air_temp_regm"),
						request.getParameter("genotype"),
						request.getParameter("height_or_length"),
						request.getParameter("plant_body_site"),
						request.getParameter("host_common_name"),
						request.getParameter("host_taxid"),
						request.getParameter("gravity"),
						request.getParameter("growth_hormone_regm"),
						request.getParameter("growth_med"),
						request.getParameter("herbicide_regm"),
						request.getParameter("humidity_regm"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("mechanical_damage"),
						request.getParameter("mineral_nutr_regm"),
						request.getParameter("non_mineral_nutr_regm"),
						request.getParameter("perturbation"),
						request.getParameter("age"),
						request.getParameter("samp_salinity"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("ph_regm"),
						request.getParameter("pesticide_regm"),
						request.getParameter("radiation_regm"),
						request.getParameter("rainfall_regm"),
						request.getParameter("salt_regm"),
						request.getParameter("season_environment"),
						request.getParameter("infra_specific_name"),
						request.getParameter("standing_water_regm"),
						request.getParameter("tot_mass"),
						request.getParameter("tiss_cult_growth_med"),
						request.getParameter("wet_mass"),
						request.getParameter("water_temp_regm"),
						request.getParameter("watering_regm"),
						request.getParameter("body_product"),
						request.getParameter("phenotype"),
						request.getParameter("plant_product"),
						request.getParameter("organism_count"),
						request.getParameter("misc_param"),
						request.getParameter("life_stage"),
						request.getParameter("infra_specific_rank"));
				environment = plant_associated;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("miscellaneous")) {
				Miscellaneous miscellaneous = new Miscellaneous(
						request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("diss_inorg_carb"),
						request.getParameter("diss_org_nitro"),
						request.getParameter("elev"),
						request.getParameter("sodium"),
						request.getParameter("temp"),
						request.getParameter("ph"),
						request.getParameter("potassium"),
						request.getParameter("alkalinity"),
						request.getParameter("ammonium"),
						request.getParameter("calcium"),
						request.getParameter("chlorophyll"),
						request.getParameter("sulfate"),
						request.getParameter("sulfide"),
						request.getParameter("diether_lipids"),
						request.getParameter("diss_oxygen"),
						request.getParameter("nitrite"),
						request.getParameter("nitro"),
						request.getParameter("org_nitro"),
						request.getParameter("organism_count"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("perturbation"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("misc_param"),
						request.getParameter("density"),
						request.getParameter("nitrate"),
						request.getParameter("chem_administration"),
						request.getParameter("chloride"),
						request.getParameter("current"),
						request.getParameter("diss_carb_dioxide"),
						request.getParameter("diss_hydrogen"),
						request.getParameter("org_carb"),
						request.getParameter("org_matter"),
						request.getParameter("phosphate"),
						request.getParameter("phosplipid_fatt_acid"),
						request.getParameter("pressure"),
						request.getParameter("biomass"),
						request.getParameter("bromide"),
						request.getParameter("salinity"),
						request.getParameter("silicate"));
				environment = miscellaneous;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("biofilm")) {
				Biofilm biofilm = new Biofilm(request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("diss_inorg_carb"),
						request.getParameter("diss_org_nitro"),
						request.getParameter("elev"),
						request.getParameter("sodium"),
						request.getParameter("temp"),
						request.getParameter("water_content"),
						request.getParameter("potassium"),
						request.getParameter("pressure"),
						request.getParameter("chem_administration"),
						request.getParameter("alkyl_diethers"),
						request.getParameter("bishomohopanol"),
						request.getParameter("biomass"),
						request.getParameter("bromide"),
						request.getParameter("alkalinity"),
						request.getParameter("aminopept_act"),
						request.getParameter("ammonium"),
						request.getParameter("bacteria_carb_prod"),
						request.getParameter("calcium"),
						request.getParameter("carb_nitro_ratio"),
						request.getParameter("redox_potential"),
						request.getParameter("salinity"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("chlorophyll"),
						request.getParameter("chloride"),
						request.getParameter("diether_lipids"),
						request.getParameter("diss_carb_dioxide"),
						request.getParameter("diss_hydrogen"),
						request.getParameter("diss_org_carb"),
						request.getParameter("diss_oxygen"),
						request.getParameter("glucosidase_act"),
						request.getParameter("magnesium"),
						request.getParameter("mean_frict_vel"),
						request.getParameter("mean_peak_frict_vel"),
						request.getParameter("org_nitro"),
						request.getParameter("ph"),
						request.getParameter("n_alkanes"),
						request.getParameter("org_carb"),
						request.getParameter("org_matter"),
						request.getParameter("petroleum_hydrocarb"),
						request.getParameter("phosplipid_fatt_acid"),
						request.getParameter("phaeopigments"),
						request.getParameter("nitrite"),
						request.getParameter("nitro"),
						request.getParameter("organism_count"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("part_org_carb"),
						request.getParameter("perturbation"),
						request.getParameter("phosphate"),
						request.getParameter("methane"),
						request.getParameter("nitrate"),
						request.getParameter("samp_store_temp"),
						request.getParameter("silicate"),
						request.getParameter("tot_carb"),
						request.getParameter("tot_org_carb"),
						request.getParameter("turbidity"),
						request.getParameter("sulfate"),
						request.getParameter("sulfide"),
						request.getParameter("misc_param"),
						request.getParameter("tot_nitro"));
				environment = biofilm;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("human_vaginal")) {
				Human_vaginal human_vaginal = new Human_vaginal(
						request.getParameter("temp"),
						request.getParameter("menarche"),
						request.getParameter("sexual_act"),
						request.getParameter("pregnancy"),
						request.getParameter("douche"),
						request.getParameter("birth_control"),
						request.getParameter("menopause"),
						request.getParameter("hrt"),
						request.getParameter("hysterectomy"),
						request.getParameter("gynecologic_disord"),
						request.getParameter("urogenit_disord"),
						request.getParameter("host_subject_id"),
						request.getParameter("age"),
						request.getParameter("sex"),
						request.getParameter("disease_stat"),
						request.getParameter("ihmc_medication_code"),
						request.getParameter("chem_administration"),
						request.getParameter("body_site"),
						request.getParameter("body_product"),
						request.getParameter("tot_mass"),
						request.getParameter("height"),
						request.getParameter("diet"),
						request.getParameter("last_meal"),
						request.getParameter("family_relationship"),
						request.getParameter("genotype"),
						request.getParameter("phenotype"),
						request.getParameter("host_body_temp"),
						request.getParameter("body_mass_index"),
						request.getParameter("ihmc_ethnicity"),
						request.getParameter("occupation"),
						request.getParameter("medic_hist_perform"),
						request.getParameter("pulse"),
						request.getParameter("perturbation"),
						request.getParameter("samp_salinity"),
						request.getParameter("organism_count"),
						request.getParameter("samp_store_temp"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_dur"),
						request.getParameter("misc_param"),
						request.getParameter("oxy_stat_samp"));
				environment = human_vaginal;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("human_skin")) {
				Human_skin human_skin = new Human_skin(
						request.getParameter("temp"),
						request.getParameter("dermatology_disord"),
						request.getParameter("time_since_last_wash"),
						request.getParameter("dominant_hand"),
						request.getParameter("host_subject_id"),
						request.getParameter("ihmc_medication_code"),
						request.getParameter("age"),
						request.getParameter("sex"),
						request.getParameter("body_site"),
						request.getParameter("body_product"),
						request.getParameter("tot_mass"),
						request.getParameter("height"),
						request.getParameter("diet"),
						request.getParameter("last_meal"),
						request.getParameter("genotype"),
						request.getParameter("phenotype"),
						request.getParameter("host_body_temp"),
						request.getParameter("body_mass_index"),
						request.getParameter("ihmc_ethnicity"),
						request.getParameter("occupation"),
						request.getParameter("medic_hist_perform"),
						request.getParameter("pulse"),
						request.getParameter("perturbation"),
						request.getParameter("samp_salinity"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("samp_store_temp"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("disease_stat"),
						request.getParameter("chem_administration"),
						request.getParameter("family_relationship"),
						request.getParameter("organism_count"),
						request.getParameter("misc_param"));
				environment = human_skin;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("human_oral")) {
				Human_oral human_oral = new Human_oral(
						request.getParameter("temp"),
						request.getParameter("nose_mouth_teeth_throat_disord"),
						request.getParameter("time_last_toothbrush"),
						request.getParameter("host_subject_id"),
						request.getParameter("age"),
						request.getParameter("sex"),
						request.getParameter("ihmc_medication_code"),
						request.getParameter("body_site"),
						request.getParameter("body_product"),
						request.getParameter("tot_mass"),
						request.getParameter("height"),
						request.getParameter("diet"),
						request.getParameter("last_meal"),
						request.getParameter("genotype"),
						request.getParameter("phenotype"),
						request.getParameter("host_body_temp"),
						request.getParameter("body_mass_index"),
						request.getParameter("ihmc_ethnicity"),
						request.getParameter("occupation"),
						request.getParameter("medic_hist_perform"),
						request.getParameter("pulse"),
						request.getParameter("perturbation"),
						request.getParameter("samp_salinity"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("samp_store_temp"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("disease_stat"),
						request.getParameter("chem_administration"),
						request.getParameter("family_relationship"),
						request.getParameter("organism_count"),
						request.getParameter("misc_param"));
				environment = human_oral;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("human_gut")) {
				Human_gut human_gut = new Human_gut(
						request.getParameter("temp"),
						request.getParameter("special_diet"),
						request.getParameter("liver_disord"),
						request.getParameter("age"),
						request.getParameter("ihmc_medication_code"),
						request.getParameter("body_site"),
						request.getParameter("body_product"),
						request.getParameter("height"),
						request.getParameter("diet"),
						request.getParameter("genotype"),
						request.getParameter("perturbation"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("host_subject_id"),
						request.getParameter("disease_stat"),
						request.getParameter("chem_administration"),
						request.getParameter("tot_mass"),
						request.getParameter("family_relationship"),
						request.getParameter("phenotype"),
						request.getParameter("pulse"),
						request.getParameter("samp_salinity"),
						request.getParameter("organism_count"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("sex"),
						request.getParameter("gastrointest_disord"),
						request.getParameter("misc_param"),
						request.getParameter("last_meal"),
						request.getParameter("host_body_temp"),
						request.getParameter("body_mass_index"),
						request.getParameter("ihmc_ethnicity"),
						request.getParameter("occupation"),
						request.getParameter("medic_hist_perform"));
				environment = human_gut;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("human_associated")) {
				Human_associated human_associated = new Human_associated(
						request.getParameter("temp"),
						request.getParameter("disease_stat"),
						request.getParameter("chem_administration"),
						request.getParameter("body_site"),
						request.getParameter("diet"),
						request.getParameter("family_relationship"),
						request.getParameter("age"),
						request.getParameter("body_mass_index"),
						request.getParameter("host_subject_id"),
						request.getParameter("height"),
						request.getParameter("last_meal"),
						request.getParameter("genotype"),
						request.getParameter("occupation"),
						request.getParameter("ihmc_medication_code"),
						request.getParameter("medic_hist_perform"),
						request.getParameter("ihmc_ethnicity"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("pulse"),
						request.getParameter("sex"),
						request.getParameter("perturbation"),
						request.getParameter("samp_salinity"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_dur"),
						request.getParameter("smoker"),
						request.getParameter("tot_mass"),
						request.getParameter("hiv_stat"),
						request.getParameter("drug_usage"),
						request.getParameter("weight_loss_3_month"),
						request.getParameter("pet_farm_animal"),
						request.getParameter("travel_out_six_month"),
						request.getParameter("twin_sibling"),
						request.getParameter("study_complt_stat"),
						request.getParameter("pulmonary_disord"),
						request.getParameter("nose_throat_disord"),
						request.getParameter("gestation_state"),
						request.getParameter("blood_blood_disord"),
						request.getParameter("maternal_health_stat"),
						request.getParameter("foetal_health_stat"),
						request.getParameter("kidney_disord"),
						request.getParameter("diet_last_six_month"),
						request.getParameter("urogenit_tract_disor"),
						request.getParameter("urine_collect_meth"),
						request.getParameter("amniotic_fluid_color"),
						request.getParameter("phenotype"),
						request.getParameter("body_product"),
						request.getParameter("host_body_temp"),
						request.getParameter("organism_count"),
						request.getParameter("samp_store_temp"),
						request.getParameter("misc_param"));
				environment = human_associated;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("host_associated")) {
				Host_associated host_associated = new Host_associated(
						request.getParameter("alt"),
						request.getParameter("depth"),
						request.getParameter("elev"),
						request.getParameter("temp"),
						request.getParameter("disease_stat"),
						request.getParameter("age"),
						request.getParameter("body_site"),
						request.getParameter("family_relationship"),
						request.getParameter("genotype"),
						request.getParameter("dry_mass"),
						request.getParameter("diet"),
						request.getParameter("host_common_name"),
						request.getParameter("host_taxid"),
						request.getParameter("host_subject_id"),
						request.getParameter("last_meal"),
						request.getParameter("host_growth_cond"),
						request.getParameter("host_body_temp"),
						request.getParameter("height_or_length"),
						request.getParameter("host_color"),
						request.getParameter("host_shape"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("infra_specific_name"),
						request.getParameter("sex"),
						request.getParameter("perturbation"),
						request.getParameter("samp_salinity"),
						request.getParameter("samp_store_temp"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("tot_mass"),
						request.getParameter("substrate"),
						request.getParameter("gravidity"),
						request.getParameter("body_habitat"),
						request.getParameter("organism_count"),
						request.getParameter("phenotype"),
						request.getParameter("chem_administration"),
						request.getParameter("body_product"),
						request.getParameter("misc_param"),
						request.getParameter("life_stage"),
						request.getParameter("infra_specific_rank"),
						request.getParameter("blood_press_diast"),
						request.getParameter("blood_press_syst"));
				environment = host_associated;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		try {
			if (env_type.equals("air")) {
				Air air = new Air(request.getParameter("alt"),
						request.getParameter("elev"),
						request.getParameter("temp"),
						request.getParameter("carb_dioxide"),
						request.getParameter("carb_monoxide"),
						request.getParameter("chem_administration"),
						request.getParameter("barometric_press"),
						request.getParameter("humidity"),
						request.getParameter("oxy_stat_samp"),
						request.getParameter("methane"),
						request.getParameter("oxygen"),
						request.getParameter("perturbation"),
						request.getParameter("samp_salinity"),
						request.getParameter("samp_store_dur"),
						request.getParameter("samp_store_loc"),
						request.getParameter("samp_store_temp"),
						request.getParameter("pollutants"),
						request.getParameter("resp_part_matter"),
						request.getParameter("solar_irradiance"),
						request.getParameter("ventilation_rate"),
						request.getParameter("ventilation_type"),
						request.getParameter("volatile_org_comp"),
						request.getParameter("wind_direction"),
						request.getParameter("wind_speed"),
						request.getParameter("organism_count"),
						request.getParameter("misc_param"));
				environment = air;
				valide.useOfNonAsciiEnv(environment);
			}
		} catch (Exception e) {
			String errmsg = "" + e.getMessage();
			System.out.print("" + errmsg);
			ModelAndView maverr = new ModelAndView("ErrorPage");
			e.printStackTrace();
			maverr.addObject("errmsg", errmsg);
			maverr.addObject("origin", origin);
			return maverr;
		}
		List<String> currentlySelectedSIDs = (List<String>) session
				.getAttribute("currentlySelectedSIDs");

		if (!origin.equals("cd_change")) {
			for (int j = 0; j < cdeLIST.size(); j++) {
				if (cdeLIST.get(j).getFirstSequenceID().trim()
						.equals(currentlySelectedSIDs.get(0).trim())) {
					cdelement = new CDElement(currentlySelectedSIDs.get(0),
							currentlySelectedSIDs.get(currentlySelectedSIDs
									.size() - 1), currentlySelectedSIDs.size(),
							studytype, environment, fastaheader, keepheader);
					cdeLIST.set(j, cdelement);
				}
			}
		} else {
			int gID = (Integer) session.getAttribute("gID");
			cdeLIST.get(gID).setCDFastaheader(fastaheader);
			cdeLIST.get(gID).setStudy(studytype);
			cdeLIST.get(gID).setEnv(environment);
		}

		session.setAttribute("cdeLIST", cdeLIST);

		if (opt_2b.equals("oneORmany") & uSeqID.size() == allSequenceIDs.size()) {
			session.setAttribute("opt_2b", "one");
		}
		if (origin.equals("3b2") & uSeqID.size() != allSequenceIDs.size()) {
			session.setAttribute("opt_2b", "many");
		}

		String init = (String) session.getAttribute("initial");
		// FIRST STEP
		if (init.equals("true")) {
			old_cdeLIST = cdeLIST;
			session.setAttribute("old_cdeLIST", old_cdeLIST);
		}

		// SECOND STEP
		if (init.equals("false") | origin.equals("cd_change")
				| context_exists.equals("yes")) { // confirm case
			old_cdeLIST = cdeLIST;
			session.setAttribute("old_cdeLIST", old_cdeLIST);
		}

		session.setAttribute("cdeLIST", cdeLIST);

		// END GO TO NEXT *
		ModelAndView mav = null;
		if (submitbutton.equals("download filled csv")) {
			CSVHandler csvh = new CSVHandler();
			csvh.generateFilledCSV(cdeLIST.get(cdeLIST.size() - 1),
					session.getId());
			mav = new ModelAndView("CSV_CREATION");
			mav.addObject("uSeqID", uSeqID);
			mav.addObject("origin", origin);
			mav.addObject("sSeqID", sSeqID);
			return (mav);
		}

		// move later
		List<String> usedSequenceIDs = (List<String>) session
				.getAttribute("usedSequenceIDs");

		if (origin.equals("cd_change")) {
			mav = new ModelAndView("cd_changes_in_x");
			return mav;
		}

		if ((uSeqID.size() == allSequenceIDs.size())
				| allSequenceIDs.size() == usedSequenceIDs.size()) {
			mav = new ModelAndView("4_MAPPING");
			// TODO:debug here...
			mav.addObject("origin", origin);
			return mav;
		}

		mav = new ModelAndView("3b1_CD_INPUT_ntom");
		mav.addObject("uSeqID", uSeqID);
		mav.addObject("origin", origin);
		return (mav);
	}
}